﻿/*********************************************************************
 * 	作用：扩展调色板类，用于显示两种模式的调色板
 * 
 * 		2008/04/23	修改切换方式为单击左上角颜色区块
 * 
 **********************************************************************/
package org.dstart.object.color
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import org.dstart.object.color.sub.*;

	public class ExColorObject extends Sprite
	{
		public static const CLICK_COLOR:String	= "clickColor";		//	鼠标选择颜色
		public static const CHANGE_MODEL:String	= "changeModel";	//	修改模式
		
		private var m_bgSprite:Sprite;				//	取色器背景框
		private var m_objColor:ColorListObject;		//	色块板
		private var m_objColor2:ExColorListObject;	//	第二种色块
		private var m_alphaSprite:Sprite;			//	透明色板区块
		private var m_nowSprite:Sprite;				//	当前颜色
		private var m_txtColor:TextField;			//	颜色文字内容

		private var nowSelectColor:uint;	/** 当前选择的颜色值 **/
		private var nowColorAlpha:Number;	/** 当前选择颜色的透明度（默认为1） **/	

		private var m_nowSaveColor:uint;	/** 在切换模式时保存的颜色值 **/
		private var m_nowSaveAlpha:Number;	/** 在切换模式时保存的透明度值 **/
		
		public function ExColorObject(xx:Number = 0, yy:Number = 0, isShowText:Boolean = false, isShowLight:Boolean = false)
		{
			InitObject(xx, yy, isShowText, isShowLight);
			InitListener();
		}		
		
		/** 读取当前颜色值 */
		public function get color():uint
		{
			return this.nowSelectColor;
		}
		
		/** 绘制颜色 **/
		public function set color(col:uint):void
		{
			this.nowSelectColor = col;
			this.m_nowSaveColor = col;
			
			this.m_objColor.now = col;
			this.m_objColor2.color = col;
			
			DrawRectangle(m_nowSprite, nowSelectColor, nowColorAlpha);
		}		

		/** 修改调色板模式（仅设置是否为高级颜色选择模式） **/
		public function set isLightModel(flag:Boolean):void
		{
			this.m_objColor.visible = !flag;
			this.m_objColor2.visible = flag;
				
			if(this.m_objColor2.visible)
			{
				this.m_nowSaveAlpha = this.cAlpha;
				this.m_nowSaveColor = this.color;
			}
			else
			{
				if(this.m_nowSaveAlpha && this.m_nowSaveColor)
				{
					this.color = this.m_nowSaveColor;
					this.cAlpha = this.m_nowSaveAlpha;
				}
			}
		}
		
		/** 读取模式（返回是否高级颜色拾取模式） **/
		public function get isLightModel():Boolean
		{
			return this.m_objColor2.visible;
		}
		
		/** 设置颜色透明度值 **/
		public function set cAlpha(num:Number):void
		{			
			this.nowColorAlpha = num;
			this.m_nowSaveAlpha = num;
			
			DrawRectangle(m_nowSprite, nowSelectColor, nowColorAlpha);
		}
		
		/** 取得当前颜色的透明度值 **/
		public function get cAlpha():Number
		{
			return nowColorAlpha;
		}
		
		//------------------------------------------------
		
		private function InitObject(xx:Number, yy:Number, isShowText:Boolean, isShowLight:Boolean):void
		{
			nowSelectColor = 0x000000;	
			nowColorAlpha = 1;
			
			m_bgSprite = new Sprite();
			
			m_alphaSprite = new Sprite();
			m_nowSprite = new Sprite();
			m_alphaSprite.x = m_nowSprite.x = 5;
			m_alphaSprite.y = m_nowSprite.y = 4;
			
			m_objColor = new ColorListObject(10, 5, 25);
			m_objColor2 = new ExColorListObject(160, 100);
			m_objColor2.x = 5;
			m_objColor2.y = 25;
			
			m_txtColor = new TextField();
			m_txtColor.x = 55;
			m_txtColor.y = 5;
			m_txtColor.width = 100;	
			m_txtColor.height = 16;
			m_txtColor.selectable = false;	
			m_txtColor.visible = isShowText;
			
			addChild(m_bgSprite);
			addChild(m_alphaSprite);
			addChild(m_nowSprite);
			addChild(m_objColor);
			addChild(m_objColor2);
			addChild(m_txtColor);

			this.x = xx;
			this.y = yy;
			
			m_objColor.visible = !isShowLight;
			m_objColor2.visible = isShowLight;
			
			/** 绘制颜色框底部的网状线条 **/
			m_alphaSprite.graphics.lineStyle(0, 0x666666, 0.6);
			
			for(var i:int = 0; i <= Math.floor(40/4); i++)
			{
				m_alphaSprite.graphics.moveTo(i * 4, 0);
				m_alphaSprite.graphics.lineTo(i * 4, 15);
			}

			for(var j:int = 0; j <= Math.floor(15/4); j++)
			{
				m_alphaSprite.graphics.moveTo(0, j * 4);
				m_alphaSprite.graphics.lineTo(40, j * 4);
			}
			
			/** 绘制背景色块及填充色块 **/
			DrawRectangle(m_bgSprite, 0xFFFFFF, 1, m_objColor.width + 10, m_objColor.height + 30);
			DrawRectangle(m_nowSprite, m_objColor.now, nowColorAlpha);			
		}
		
		private function InitListener():void
		{
			m_objColor.addEventListener(MouseEvent.CLICK, onColorClick);
			m_objColor2.addEventListener(ExColorListObject.CLICK_COLOR, onColorChanges);
			
			m_objColor.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveEvent);
			m_objColor.addEventListener(MouseEvent.ROLL_OUT, onRollOutEvent);
			
			// m_nowSprite.doubleClickEnabled = true;
			// m_nowSprite.addEventListener(MouseEvent.DOUBLE_CLICK, onChangeList);
			m_nowSprite.addEventListener(MouseEvent.CLICK, onChangeList);
		}
		
		/** 鼠标双击颜色区，修改颜色取色模式 **/
		private function onChangeList(e:MouseEvent):void
		{
			this.isLightModel = !this.isLightModel;
			
			this.dispatchEvent(new Event(ExColorObject.CHANGE_MODEL));
		}			

		/** 为指定 Sprite 对象绘制填充，后面的默认值是为 取色器 对象准备的 **/
		private function DrawRectangle(spr:Sprite, col:uint, alp:Number = 1, ww:Number = 40, hh:Number = 15):void
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(0, 0x000000, 1);
			spr.graphics.beginFill(col, alp);
			spr.graphics.drawRect(0, 0, ww, hh);
			spr.graphics.endFill();
		}
		
		/** 鼠标滑动时触发修改颜色事件 **/
		private function onMouseMoveEvent(e:MouseEvent):void
		{
			ShowColorBox(m_objColor.color);
		}
		
		/** 绘制当前颜色 **/
		private function onRollOutEvent(e:MouseEvent):void
		{
			ShowColorBox(nowSelectColor);
		}
		
		/** 鼠标点击颜色时触发事件（针对 256 色调色板） **/
		private function onColorClick(e:MouseEvent):void
		{			
			DrawRectangle(m_nowSprite, m_objColor.color, nowColorAlpha);

			this.nowSelectColor = m_objColor.color;
			this.m_objColor.now = m_objColor.color;
			
			this.dispatchEvent(new Event(ColorObject.CLICK_COLOR));
		}
		
		/** 颜色改变时触发事件（针对16位彩色调色板） **/
		private function onColorChanges(e:Event):void
		{
			this.ShowColorBox(m_objColor2.color);			
			this.nowSelectColor = m_objColor2.color;
			
			this.dispatchEvent(new Event(ColorObject.CLICK_COLOR));
		}
		
		/** 绘制颜色方块 **/
		private function ShowColorBox(col:uint):void
		{
			DrawRectangle(m_nowSprite, col, nowColorAlpha);			

			if(m_txtColor.visible)
			{
				m_txtColor.text = String("000000" + col.toString(16)).substr(-6).toUpperCase();
			}			
		}
	}
}